package com.doordu.soa.service.comm.utils;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSSClient;

public class OSSClientFileUtils {
	/**
	 * 上传阿里云
	 * 
	 * @Title: uploadAliyuncs
	 * @Description:
	 * @author xiaoqun.yi yixq@doordu.com
	 * @date 2017年9月25日 下午5:50:01
	 * @param accessKeyId
	 * @param accessKeySecret
	 * @param endpoint
	 * @param path
	 *            图片全地址
	 * @param companyId
	 *            公司id
	 * @param localParent
	 *            本地父目录
	 * @param ossParentPath
	 *            阿里云父目录
	 * @return
	 * @return Map<String,String>
	 */
	public static  Map<String, String> uploadAliyuncs(String accessKeyId, String accessKeySecret, String endpoint, String path, Integer companyId, String localParent, String ossParentPath) {
		Map map = getLocalFilePath(localParent, ossParentPath, path, 1);

		// 创建OSSClient实例
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		// 上传文件
		// doordu_admin/parkingLot/192.168.1.100_in/20170815185235.jpg
		ossClient.putObject("doordustorage", map.get("yourKey").toString(), new File(map.get("local").toString()));
		// 关闭client
		ossClient.shutdown();
		map.put("flag", "1");
		return map;
	}

	/**
	 * 
	 * @Title: getLocalFilePath
	 * @Description:
	 * @author xiaoqun.yi yixq@doordu.com
	 * @date 2017年9月25日 下午5:50:47
	 * @param localParentPath
	 *            本地父目录
	 * @param ossParentPath
	 *            阿里云父目录
	 * @param path
	 *            图片全地址
	 * @param companyId
	 *            公司id
	 * @return
	 * @return Map<String,String>
	 */
	public static  Map<String, String> getLocalFilePath(String localParentPath, String ossParentPath, String path, Integer companyId) {

		Map<String, String> map = new HashMap<String, String>();

		String filePath = "";
		String yourKey = ossParentPath + companyId + "/";
		try {
			URL httpurl = new URL(path);
			String fileName = path.substring(path.indexOf("/", 50) + 1);
			filePath = localParentPath + companyId + "/" + fileName;
			yourKey += fileName;
			File file = new File(filePath);
			FileUtils.copyURLToFile(httpurl, file);
		} catch (MalformedURLException e) {
			e.printStackTrace();
			filePath = "";
		} catch (IOException e) {
			e.printStackTrace();
			filePath = "";
		}
		map.put("local", filePath);
		map.put("yourKey", yourKey);
		return map;
	}

	public static void main(String[] args) {
		OSSClientFileUtils oss = new OSSClientFileUtils();
		String path = "http://lightcar.oss-cn-shenzhen.aliyuncs.com/ed1d172858ee4bdebab99b86e4e014a4/20170810/192.168.1.100_in/20170810191340.jpg";
		// endpoint以杭州为例，其它region请按实际情况填写
		String endpoint = "https://oss-cn-shenzhen.aliyuncs.com";
		// 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录
		// https://ram.console.aliyun.com 创建
		String accessKeyId = "IvFlsOddyioLNSnr";
		String accessKeySecret = "SjShWDKvsjs7EfrOMI2irzgIjw6gEB";
		Integer companyId = 1;
		String localParent = "D:/testdownload";
		String ossParentPath = "doordu_admin/parkingLot/";

		Map map = oss.uploadAliyuncs(accessKeyId, accessKeySecret, endpoint, path, companyId, localParent, ossParentPath);
		// 上传文件
		System.out.println(JSONObject.toJSON(map));

	}
}
